package org.batyrov.equery.action.admin;

import org.batyrov.equery.action.BaseAction;
import org.batyrov.equery.action.Const;
import org.batyrov.equery.bean.User;

import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 25.07.2009
 * Time: 0:02:29
 * To change this template use File | Settings | File Templates.
 */
public class RemoveUser extends BaseAction {
    private long id;
    private Collection<User> users;

    public Collection<User> getUsers() {
        return users;
    }

    public void setUsers(Collection<User> users) {
        this.users = users;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Override
    public String execute() throws Exception {
        super.execute();
        User user = em.find(User.class, id);
        if (user == null){
            addActionError(getText("user_not_found"));
            users = dao.getAllUsers();
            return Const.ERROR;
        }
        if (!dao.remove(user)) {
            addActionError(getText("cannot_delete_user"));
            users = dao.getAllUsers();
            return Const.ERROR;
        }
        return Const.SUCCESS;
    }
}
